package ui.view;

import bean.DataFrameContainer;
import bean.ModelType;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Consumer;
import joinery.DataFrame;
import org.jfree.ui.tabbedui.VerticalLayout;
import tool.DataProvider;
import tool.Lg;
import ui.frame.HBaseFrame;
import ui.listener.FilterTextListener;
import ui.listener.TableClickAction;
import ui.listener.TableClickAdapter;
import ui.model.BaseModel;


import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static tool.TableProperty.ID;

public class BaseFilterView extends BaseModel {

    private Map<String,String> filters = new HashMap<>();
    private Observable<Map<String, String>> mapObservable;
    private HScrollTableView hScrollTableView = new HScrollTableView();
    private DataFrame<String> allData;
    private DataFrameContainer container;

    public BaseFilterView(ModelType modelType) {
        super(modelType);
    }
    public void iniFilterPanel(final Map<String, JTextPane> map)
    {
//        this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
        this.setLayout(new BorderLayout());

        hScrollTableView.gethTable().setEditable(false);

        LabelTextPanel labelTextPanel = new LabelTextPanel();
        for (String key:map.keySet())
        {
            JTextPane jTextPane = map.get(key);
            jTextPane.setName(key);
            jTextPane.setPreferredSize(new Dimension(170,25));
            jTextPane.setBorder(BorderFactory.createLoweredSoftBevelBorder());
            labelTextPanel.addLabelTextPanel(key,jTextPane);
        }

        mapObservable = Observable.create(new ObservableOnSubscribe<Map<String, String>>() {
            @Override
            public void subscribe(final ObservableEmitter<Map<String, String>> ee) throws Exception {
                for (final String key : map.keySet()) {
                    final JTextPane jTextPane = map.get(key);
                    jTextPane.addKeyListener(new FilterTextListener(key) {
                        @Override
                        public void keyReleased(KeyEvent e) {
                            String text = jTextPane.getText();
                            String value = filters.get(key);
                            if (!text.equals(value)) {
                                filters.put(key, text);
                            }
                            if (text.length() == 0) {
                                filters.remove(key);
                            }
                            ee.onNext(filters);
                        }
                    });
                }
            }
        });
        this.add(labelTextPanel, HBaseFrame.Direction.NORTH);
    }

    public void iniConsume(final String...strings)
    {
        mapObservable.subscribe(new Consumer<Map<String, String>>() {
            @Override
            public void accept(final Map<String, String> stringStringMap) throws Exception {
                DataFrame<String> newData = allData.select(new DataFrame.Predicate<String>() {
                    @Override
                    public Boolean apply(List<String> strings) {
                        for (String key : stringStringMap.keySet()) {
                            int i = container.indexOfCol(key);
//                            Lg.info(key,i,String.join(";",strings));
                            String content = strings.get(i);
                            if (content == null) {
                                return false;
                            }else if (!content.contains(stringStringMap.get(key)))
                            {
                                return false;
                            }
                        }
                        return true;
                    }
                });
                hScrollTableView.reload(newData,strings);
            }
        });

    }

    public void iniTabelAction(TableClickAction tableClickAction)
    {
        TableClickAdapter tableClickAdapter = new TableClickAdapter(hScrollTableView, tableClickAction);
    }

    public void iniTable(DataFrame<String> dataFrame,String ...strings)
    {
        allData = dataFrame;
        container = new DataFrameContainer(allData);
        hScrollTableView.reload(allData,strings);
//        JPanel jPanel = new JPanel();
//        this.add(jPanel);
//        jPanel.setLayout(new BoxLayout(jPanel,BoxLayout.Y_AXIS));
//        jPanel.add(hScrollTableView);
        this.add(hScrollTableView, HBaseFrame.Direction.CENTER);

    }

}

